/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package calendarioservice;

import calendarioentities.Calendario;
import calendarioentities.Cita;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;

/**
 *
 * @author alyer
 */
@Stateless
@Path("calendario")
public class CalendarioFacadeREST extends AbstractFacade<Calendario> {
    @PersistenceContext(unitName = "calendarioRESTFULPU")
    private EntityManager em;
    @Context
    private UriInfo context;

    public CalendarioFacadeREST() {
        super(Calendario.class);
    }

    @POST
    @Override
    @Consumes({"application/xml", "application/json"})
    public void create(Calendario entity) {
        super.create(entity);
    }

    @PUT
    @Consumes({"application/xml", "application/json"})
    public Response edit(Calendario entity) {
        super.edit(entity);
        URI createduri = null;
        try {
          createduri = new URI(context.getAbsolutePath().toString()+"/"+entity.getIdcalendario());
        } catch (URISyntaxException ex) { }
        return Response.status(Response.Status.NO_CONTENT).header("Location", createduri).build();
    }

    @DELETE
    @Path("{id}")
    public void remove(@PathParam("id") Integer id) {
        super.remove(super.find(id));
    }

    @GET
    @Path("{id}")
    @Produces({"application/xml", "application/json"})
    public Calendario find(@PathParam("id") Integer id) {
        return super.find(id);
    }

    @GET
    @Override
    @Produces({"application/xml", "application/json"})
    public List<Calendario> findAll() {
        return super.findAll();
    }

    @GET
    @Path("{from}/{to}")
    @Produces({"application/xml", "application/json"})
    public List<Calendario> findRange(@PathParam("from") Integer from, @PathParam("to") Integer to) {
        return super.findRange(new int[]{from, to});
    }

    @GET
    @Path("count")
    @Produces("text/plain")
    public String countREST() {
        return String.valueOf(super.count());
    }
    
    @Override
    protected EntityManager getEntityManager() {
        return em;
    }
     /* ****************************************************** 
     * Creacion de nuevos metodos Trabajo 2 - RESTFul    
     * Alexander Romero 
     * Daniel Vasquez
     ***************************************************** */
    
    /**
     * @return 
     * @descripcion: obtenemos todos los calendarios publicos que
     *               existen
     */
    @GET
    @Path("publicos")
    @Produces({"application/xml", "application/json"})
    public Collection<Calendario> findCalendariosPublicos(){
        boolean publico=true;
        Collection<Calendario> calendariosPublicos=null;             
        javax.persistence.Query q = getEntityManager().createNamedQuery("Calendario.findByPublico");
        q.setParameter("publico",publico);
        calendariosPublicos=q.getResultList();
        return calendariosPublicos;
        
    }   
     /**
     * @descripcion: devuelve las citas de acuerdo al calendario consultado
     * @param idCalendario
     * @return 
     */
    @GET
    @Path("{id}/citas")
    @Produces({"application/xml", "application/json"})
    public Collection<Cita> findByCalendariosId(@PathParam("id") Integer idCalendario){
        Collection<Cita> citasCalendario=null;
        javax.persistence.Query q = getEntityManager().createNamedQuery("Cita.findByIdCalendario");
        q.setParameter("idcalendario",new Calendario(idCalendario));
        citasCalendario=q.getResultList();
        return citasCalendario;
    }
}
